package com.psy.counseling.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.psy.counseling.entity.Situation;
import com.psy.counseling.service.SituationService;
import com.psy.counseling.utils.Result;
import com.psy.counseling.vo.DeleteVO;
import com.psy.counseling.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author 陈兴海
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/api/situation")
public class SituationController {
    @Autowired
    private SituationService situationService;

    /**
     * 添加咨询情况
     *
     * @param situation
     * @return
     */
    @PostMapping("/addSituation")
    public Result<?> addSituation(@RequestBody Situation situation) {
        return situationService.addSituation(situation);
    }

    /**
     * 修改咨询情况
     *
     * @param situation
     * @return
     */
    @PutMapping("/updateSituation")
    public Result<?> updateSituation(@RequestBody Situation situation) {
        return situationService.updateSituation(situation);
    }

    /**
     * 删除咨询情况信息
     *
     * @param deleteVO
     * @param flag
     * @return
     */
    @DeleteMapping("/deleteSituation/{flag}")
    public Result<?> deleteSituation(@RequestBody DeleteVO deleteVO, @PathVariable("flag") Integer flag) {
        return situationService.deleteSituation(deleteVO, flag);
    }

    /**
     * 获取咨询详情
     *
     * @param id
     * @return
     */
    @GetMapping("/getSituation/{id}")
    public Result<Situation> getSituation(@PathVariable("id") Long id) {
        return situationService.getSituation(id);
    }

    /**
     * 获取咨询已咨询次数
     *
     * @param askId
     * @return
     */
    @GetMapping("/getSituationNumber/{askId}")
    public Result<Integer> getSituationNumber(@PathVariable("askId") Long askId) {
        Integer count = situationService.getCount(askId);
        return Result.success(count);
    }

    /**
     * 通过咨询预约id查询对应咨询情况
     *
     * @param askId
     * @return
     */
    @GetMapping("/listSituations/{askId}")
    public Result<Page<Situation>> listSituations(@PathVariable("askId") Long askId, PageVO pageVO) {
        if (askId != null) {
            Page<Situation> page = new Page<>(pageVO.getPageNum(), pageVO.getPageSize());
            LambdaQueryWrapper<Situation> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Situation::getAskId, askId);
            page = situationService.page(page, wrapper);
            return Result.success(page);
        }
        return Result.error("204", "查询失败");
    }


}

